package com.xoms.admin.persist.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.xoms.admin.model.RoleBean;
import com.xoms.admin.model.vo.RoleResourceBean;
import com.xoms.admin.persist.dao.RoleDao;
import com.xoms.util.StringUtil;

@Repository
public class RoleImpl extends BaseImpl implements RoleDao {
	
	final String NAME_SPACE = NAME_SPACE_HEADER + ".RoleMapper";

	@Override
	public List<RoleBean> selectRoles2Account(String accId) {
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectRoles2Account", accId);
	}

	@Override
	public int selectCount2Account(String accid) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectCount2Account", accid);
	}

	@Override
	public List<RoleResourceBean> selectRoleResouces2Account(String accId) {
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectRoleResouces2Account", accId);
	}

	@Override
	public int insertAccountRoles(String aid, String accid, String[] roleIds) {
		Map<String, Object> params = new HashMap<String, Object>();
		List<Map<String, Object>> accRoles = new ArrayList<Map<String,Object>>();
		for(String rid : roleIds) {
			Map<String, Object> ar = new HashMap<String, Object>();
			ar.put("aid", aid);
			ar.put("accid", accid);
			ar.put("roleId", rid);
			accRoles.add(ar);
		}
		params.put("accRoles", accRoles);
		return sqlSessionTemplate.insert(NAME_SPACE + ".insertAccountRoles", params);
	}

	@Override
	public int delAccountRoles(String accid, String[] roleIds) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("accid", accid);
		params.put("roleIds", roleIds);
		return sqlSessionTemplate.delete(NAME_SPACE + ".delAccountRoles", params);
	}

	@Override
	public int insert(RoleBean data) {
		return sqlSessionTemplate.insert(NAME_SPACE + ".insert", data);
	}

	@Override
	public int update(RoleBean data) {
		return sqlSessionTemplate.update(NAME_SPACE + ".update", data);
	}

	@Override
	public RoleBean selectOneById(Integer id) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectOneById", id);
	}

	@Override
	public int updateStatus(int roleId, int status) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("roleId", roleId);
		params.put("status", status);
		return sqlSessionTemplate.update(NAME_SPACE + ".updateStatus", params);
	}

	@Override
	public int del(int roleId) {
		return sqlSessionTemplate.delete(NAME_SPACE + ".delete", roleId);
	}

	@Override
	public int selectAccNum2Role(int roleId) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectAccNum2Role", roleId);
	}

	@Override
	public int delAllRoleRelation(int roleId) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".delAllRoleRelation", roleId);
	}

	@Override
	public int selectResNum2Role(int roleId) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectResNum2Role", roleId);

	}

	@Override
	public int delAllResRelation2Role(int roleId) {
		return sqlSessionTemplate.delete(NAME_SPACE + ".delAllResRelation2Role", roleId);
	}

	@Override
	public int selectCount(String key) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (!StringUtil.isEmpty(key)) {
			params.put("key", key);
		}
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectCount", params);
	}

	@Override
	public List<RoleBean> selectList(String key, int page, int pagesize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("start", page * pagesize);
		params.put("length", pagesize);
		if (!StringUtil.isEmpty(key)) {
			params.put("key", key);
		}

		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectList", params);

	}

	@Override
	public int insertRoleResources(String aid, int roleId, String[] resourceIds) {
		Map<String, Object> params = new HashMap<String, Object>();
		List<Map<String, Object>> roleRes = new ArrayList<Map<String,Object>>();
		for(String rid : resourceIds) {
			Map<String, Object> rr = new HashMap<String, Object>();
			rr.put("aid", aid);
			rr.put("roleId", roleId);
			rr.put("resourceId", rid);
			roleRes.add(rr);
		}
		params.put("roleRes", roleRes);
		return sqlSessionTemplate.insert(NAME_SPACE + ".insertRoleResources", params);
	}

	@Override
	public int updateSorts(List<String> sortList, int page, int pagesize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("start", page * pagesize);
		params.put("length", pagesize);
		params.put("sortList", sortList);
		
		return sqlSessionTemplate.update(NAME_SPACE + ".updateSorts", params);
	}

	@Override
	public int delRoleResources(int roleId, String[] rids) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("roleId", roleId);
		params.put("rids", rids);
		return sqlSessionTemplate.insert(NAME_SPACE + ".delRoleResources", params);
	}


}
